﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.ComponentModel
Imports System.Drawing
Imports System.Threading

Public Class actualiza_notas
    Private Function TimeConsumingOperation( _
 bw As BackgroundWorker, _
 sleepPeriod As Integer) As Integer

        Dim result As Integer = 0

        Dim rand As New Random()

        While Not bw.CancellationPending
            Dim [exit] As Boolean = False

            Select Case rand.Next(3)
                ' Raise an exception.
                Case 0
                    Throw New Exception("An error condition occurred.")
                    Exit While

                    ' Sleep for the number of milliseconds
                    ' specified by the sleepPeriod parameter.
                Case 1
                    Thread.Sleep(sleepPeriod)
                    Exit While

                    ' Exit and return normally.
                Case 2
                    result = 23
                    [exit] = True
                    Exit While

                Case Else
                    Exit While
            End Select

            If [exit] Then
                Exit While
            End If
        End While

        Return result
    End Function
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.BackgroundWorker1.CancelAsync()

        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ProgressBar1.Visible = True

        Me.BackgroundWorker1.RunWorkerAsync(2000)
        Button1.Enabled = False
        Button2.Enabled = False
        Dim SQL1 As String = "delete from Notas"
        Conn.Open()
        Try
            Dim cdo As New SqlCommand(SQL1, Conn)
            cdo.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox("Error al limpiar tabla: " & ex.Message, MsgBoxStyle.Critical)
        End Try
        Conn.Close()
        Dim Ruc, Año, Mes, Dia, Nota, Hora, Usuario, Fecha, hor, min, seg As String
        Dim fs As Scripting.FileSystemObject
        Dim ts1 As Scripting.TextStream
        fs = New Scripting.FileSystemObject
        ts1 = fs.OpenTextFile("O:\Sistemas\filetxt\VENNOT.txt", Scripting.IOMode.ForAppending, False, Scripting.Tristate.TristateUseDefault)
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = ts1.Line
        ts1.Close()
        Dim ts = File.OpenText("O:\Sistemas\filetxt\VENNOT.txt")
        Dim Array() As String
        Static X
        Do While ts.Peek <> -1
            Dim LVItem As New ListViewItem
            Array = ts.ReadLine.Split(vbTab)
            If Len(Array(0).ToString) <> 0 Then
                Ruc = RTrim(Array(0).ToString)
                Año = Mid(Array(1).ToString, 1, 4)
                Mes = Mid(Array(1).ToString, 5, 2)
                Dia = Mid(Array(1).ToString, 7, 2)
                Fecha = Dia & "/" & Mes & "/" & Año
                hor = Mid(Array(2).ToString, 1, 2)
                min = Mid(Array(2).ToString, 3, 2)
                seg = Mid(Array(2).ToString, 5, 2)
                Hora = hor & ":" & min & ":" & seg
                Nota = RTrim(Array(3).ToString)
                Nota = Nota.Replace("'", "''")
                Usuario = RTrim(Array(5).ToString)
                Dim SQL As String = "INSERT INTO Notas VALUES('" & Ruc & "', '" & Format(CType(Fecha, Date), "MM/dd/yyyy") & "', '" & Hora & "', '" & Nota & "', '" & Usuario & "'" & ")"
                Conn.Open()
                Try
                    Dim cdo As New SqlCommand(SQL, Conn)
                    cdo.ExecuteNonQuery()
                    X = X + 1
                    ProgressBar1.Value = X
                    Label2.Text = X
                Catch ex As Exception
                    MsgBox("Error al grabar: " & ex.Message, MsgBoxStyle.Critical)
                End Try
                Conn.Close()
            End If
        Loop
        ts.Close()
        MsgBox("Actualización completa", MsgBoxStyle.Exclamation)
        Me.Close()
    End Sub

    Private Sub actualiza_notas_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub BackgroundWorker1_DoWork_1(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim bw As BackgroundWorker = CType(sender, BackgroundWorker)

        ' Extract the argument.
        Dim arg As Integer = Fix(e.Argument)

        ' Start the time-consuming operation.
        e.Result = TimeConsumingOperation(bw, arg)

        ' If the operation was canceled by the user, 
        ' set the DoWorkEventArgs.Cancel property to true.
        If bw.CancellationPending Then
            e.Cancel = True
        End If
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        If e.Cancelled Then
            ' The user canceled the operation.
            MessageBox.Show("Operation was canceled")
        ElseIf (e.Error IsNot Nothing) Then
            ' There was an error during the operation.
            Dim msg As String = String.Format("An error occurred: {0}", e.Error.Message)
            MessageBox.Show(msg)
        Else
            ' The operation completed normally.
            Dim msg As String = String.Format("Result = {0}", e.Result)
            MessageBox.Show(msg)
        End If
    End Sub
End Class